#include <cstdio>
#include <iostream>
#include <windows.h> // 用于控制台颜色设置
#include <thread>    // 用于延时
#include <chrono>    // 用于时间控制
#include <string>    // 用于字符串操作
#include <limits>    // 添加这一行


// 闪烁显示创纪录消息
void flashRecordMessage(const std::string& message) {
    const WORD colors[] = {
        FOREGROUND_RED | FOREGROUND_INTENSITY,
        FOREGROUND_GREEN | FOREGROUND_INTENSITY,
        FOREGROUND_BLUE | FOREGROUND_INTENSITY,
        FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY
    };

    for (int i = 0; i < 4; ++i) {
        for (WORD color : colors) {
            setColor(color);
            std::cout << "\r" << message << std::flush;
            std::this_thread::sleep_for(std::chrono::milliseconds(200));
        }
    }

    // 恢复默认颜色
    setColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    std::cout << std::endl;
}

void flashTrophy() {
    const int flashTimes = 3; // 闪烁次数
    const WORD yellow = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY;
    const WORD white = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY;

    // 读取奖杯内容到vector中
    std::ifstream file("trophy.txt");
    std::vector<std::string> trophyLines;
    std::string line;
    while (std::getline(file, line)) {
        trophyLines.push_back(line);
    }
    file.close();

    // 保存原始光标位置
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
    COORD originalPos = csbi.dwCursorPosition;

    for (int i = 0; i < flashTimes; ++i) {
        // 黄色显示
        setColor(yellow);
        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), originalPos);
        for (const auto& trophyLine : trophyLines) {
            std::cout << "                      " << trophyLine << std::endl;
        }
        std::this_thread::sleep_for(std::chrono::milliseconds(300));

        // 白色显示
        setColor(white);
        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), originalPos);
        for (const auto& trophyLine : trophyLines) {
            std::cout << "                      " << trophyLine << std::endl;
        }
        std::this_thread::sleep_for(std::chrono::milliseconds(300));
    }

    // 最终以黄色显示
    setColor(yellow);
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), originalPos);
    for (const auto& trophyLine : trophyLines) {
        std::cout << "                      " << trophyLine << std::endl;
    }

    // 恢复默认颜色
    setColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
}

void printTrophy() {
    std::cout << std::endl;
    std::cout << "                      给你小奖杯！" << std::endl;
    flashTrophy();
    setColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);// 恢复默认颜色
    std::cout << std::endl;
}

void printendpage() {
    const char* recordFileName = "record.txt";
    int max_score = 0;
    setColor(15);
    std::cout << "                      *============================*" << std::endl;
    std::cout << "                      |          游戏结束          |" << std::endl;
    std::cout << "                      *============================*" << std::endl;
    gotoxy(3, 10);
    std::cout << "                      游戏得分：" << score << "分" << std::endl;
    std::cout << std::endl;
    // 读取最高分
    bool readSuccess = readMaxScore(recordFileName, max_score);

    if (!readSuccess) {
        // 如果文件不存在，创建并初始化
        if (!writeMaxScore(recordFileName, 0)) {
            std::cerr << "无法创建或写入记录文件" << std::endl;
            return;
        }
        max_score = 0;
    }

    if (score <= max_score) {
        setColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
        std::cout << "                      继续努力吧~你离最高分还差" << max_score - score
            << "分(>_<)" << std::endl;
        setColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    }
    else {
        // 创纪录消息
        flashRecordMessage("                      创纪录啦！最高分被你刷新啦，真棒！！！");
        // 打印奖杯
        printTrophy();

        // 更新最高分
        if (!writeMaxScore(recordFileName, score)) {
            std::cerr << "更新最高分失败" << std::endl;
        }
    }
}

void gameover() {
    system("cls");
    printendpage();
    std::cout << std::endl;

    std::cout << "                            按0返回主菜单" << std::endl;
    std::cout << "                            按1重新开始游戏" << std::endl;
    std::cout << "                            按2查看最高分" << std::endl;
    std::cout << "                            按4退出游戏" << std::endl;
    
    while (1) {
        for (int i = 1;i <= refresh_interval;i++)++score;
        for (int i = 1;i <= refresh_interval;i++)--score;
        if (GetAsyncKeyState('0') & 0x8000) {
			mode = 0;
			break;
        }
        if (GetAsyncKeyState('1') & 0x8000) {
			mode = 1;
            break;
        }
        if (GetAsyncKeyState('2') & 0x8000) {
			mode = 2;
			break;
        }
        if (GetAsyncKeyState('4') & 0x8000) {
            mode = 4;
            break;
        }
    }
}